# -*- coding: utf-8 -*-
import os
import sqlite3

class NewsDB(object):

	def __init__(self,workspace):
		self.dbFilePath = workspace + os.sep + "db" + os.sep + "newsDB.db"

		if not os.path.exists(self.dbFilePath):
			file = open(self.dbFilePath,"w")
			self.conn = sqlite3.connect(self.dbFilePath)
			self.cursor = self.conn.cursor()
			self.cursor.execute("CREATE TABLE news(title TEXT, summary TEXT, link TEXT, updat TEXT, category TEXT)")			
			self.conn.commit()
		else:
			self.conn = None
			self.conn = sqlite3.connect(self.dbFilePath)
			self.cursor = self.conn.cursor()
	
	def loadNews(self):
		self.cursor.execute("SELECT title,summary,link,updat,category FROM news")
		storedNews = self.cursor.fetchall()
		return storedNews

	def addNews(self,title,summary,link,update,category):
		title = unicode(title)
		summary = unicode(summary)
		category = unicode(category)
		link = unicode(link)
		update = unicode(update)
		params = (title, summary, link, update, category,)
		self.cursor.execute("INSERT INTO news VALUES(?,?,?,?,?)", params)
		self.conn.commit()